package com.lianzai.system.controller;

import java.util.List;
import java.util.Map;

import com.lianzai.common.annotation.NoLoginAuthRequired;
import com.lianzai.common.utils.QueryDO;
import com.lianzai.mobile.domain.CommunityInfoDO;
import com.lianzai.mobile.service.CommunityInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.lianzai.common.utils.PageUtils;
import com.lianzai.common.utils.Query;
import com.lianzai.common.utils.R;

/**
 * 社区表
 * 
 * @author zb
 * @email 1106900429@qq.com
 * @date 2018-12-31 19:08:21
 */
 
@Controller
@RequestMapping("/system/communityInfo")
public class CommunityInfoController {

	@Autowired
	private CommunityInfoService communityInfoService;
	
	@GetMapping()
	@RequiresPermissions("system:communityInfo:communityInfo")
	String CommunityInfo(){
	    return "system/communityInfo/communityInfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CommunityInfoDO> communityInfoList = communityInfoService.list(query);
		int total = communityInfoService.count(query);
		PageUtils pageUtils = new PageUtils(communityInfoList, total);
		return pageUtils;
	}

	@NoLoginAuthRequired
	@ResponseBody
	@PostMapping("/getList")
	public PageUtils getList(@RequestBody QueryDO queryDO){
		//查询列表数据
		Query query = new Query(queryDO);
		List<CommunityInfoDO> communityInfoList = communityInfoService.list(query);
		int total = communityInfoService.count(query);
		PageUtils pageUtils = new PageUtils(communityInfoList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:communityInfo:add")
	String add(){
	    return "system/communityInfo/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:communityInfo:edit")
	String edit(@PathVariable("id") Integer id, Model model){
		CommunityInfoDO communityInfo = communityInfoService.get(id);
		model.addAttribute("communityInfo", communityInfo);
	    return "system/communityInfo/edit";
	}

	@GetMapping("/rule/{id}")
	String rule(@PathVariable("id") Integer id, Model model){
		//查询规则列表
		CommunityInfoDO communityInfo = communityInfoService.get(id);
		model.addAttribute("communityInfo", communityInfo);
		return "system/communityInfo/addRule";
	}

	@GetMapping("/ticket/{id}")
	String ticket(@PathVariable("id") Integer id, Model model){
		CommunityInfoDO communityInfo = communityInfoService.get(id);
		model.addAttribute("communityInfo", communityInfo);
		model.addAttribute("id", id);
		return "system/communityInfo/ticket";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:communityInfo:add")
	public R save( CommunityInfoDO communityInfo){
		if(communityInfoService.save(communityInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:communityInfo:edit")
	public R update( CommunityInfoDO communityInfo){
		communityInfoService.update(communityInfo);
		return R.ok();
	}

    @ResponseBody
    @PostMapping("/sync")
    public R sync(Integer id){
        int ret = communityInfoService.syncToLocalSystem(id);

        if (ret > 0) {
            return R.ok("同步命令已执行");
        } else {
            return R.error();
        }
    }

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:communityInfo:remove")
	public R remove(Integer id){
		if(communityInfoService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:communityInfo:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		communityInfoService.batchRemove(ids);
		return R.ok();
	}
	
}
